/*
 * Formation.h
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#pragma once

#include "Soldier.h"
#include <string>

class Formation
{
public:
    Formation(const SDL_Rect& area) :
        _area(area),
        _units(NULL),
        _cols(0),
        _rows(0),
        _population(0),
        _width(0),
        _height(0)
    {}

    ~Formation() { clear(); }

    void load(const std::string& data);
    void blit(SDL_Surface* screen);
    void erase(SDL_Surface* screen, SDL_Surface* background);
    void update(void* arg);
    void move(int dx, int dy);
    void clear();
    Soldier* clean(unsigned int x, unsigned int y);

    Soldier* get(unsigned int x, unsigned int y)   { return _units[x + y * _cols]; }

    unsigned int population() const { return _population; }
    unsigned int width()      const { return _width;  }
    unsigned int height()     const { return _height; }
    unsigned int cols()       const { return _cols; }
    unsigned int rows()       const { return _rows; }

private:
    void detect_dimensions(const std::string& data);

private:
    const SDL_Rect& _area;
    Soldier** _units;
    unsigned int _cols;
    unsigned int _rows;
    unsigned int _population;
    unsigned int _width;
    unsigned int _height;
};

